package orange.com.openstack.sahara.api;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import net.minidev.json.JSONObject;

import com.google.gson.Gson;
import com.jayway.jsonpath.JsonPath;

import orange.com.openstack.sahara.data.Cluster;
import orange.com.openstack.sahara.httpConnection.RestCall;

/**
 * class allowing the interaction with the cluster Rest service
 * 
 * @author osvaldo.cocucci@gmail.com
 * 
 */
public class Clusters {

	String endpoint;
	String tenantId;
	String token;
	Gson gson;

	/**
	 * @param endpoint
	 * @param tenantId
	 * @param token
	 * 
	 *            constructor
	 */
	public Clusters(String endpoint, String tenantId, String token) {
		gson = new Gson();
		this.endpoint = endpoint;
		this.tenantId = tenantId;
		this.token = token;
	}

	/**
	 * 
	 * This operation returns the list of all Clusters.
	 */
	public List<Cluster> listCluster() {
		List<Cluster> dataSources = new ArrayList<Cluster>();
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/clusters", null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			List<JSONObject> dataSourcesJSON = JsonPath.read(JSONreponse,
					"$.clusters[*]");

			for (JSONObject json : dataSourcesJSON) {
				dataSources.add(gson.fromJson(json.toJSONString(),
						Cluster.class));
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dataSources;
	}

	/**
	 * @param dataSourceId
	 * 
	 *            This operation shows information about a specified Cluster.
	 */
	public Cluster showCluster(String clusterId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/clusters/" + clusterId, null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();

			return gson.fromJson(((JSONObject) JsonPath.read(JSONreponse,
					"$.cluster")).toJSONString(), Cluster.class);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param plugin_name
	 * @param hadoop_version
	 * @param cluster_template_id
	 * @param default_image_id
	 * @param user_keypair_id
	 * @param name
	 *            e.g "plugin_name": "vanilla", "hadoop_version": "1.2.1",
	 *            "cluster_template_id": "1bb1cced-765e-4a2b-a5b6-ac6bbb0bb798",
	 *            "default_image_id": "db12c199-d0b5-47d3-8a97-e95eeaeae615",
	 *            "user_keypair_id": "doc-keypair", "name": "doc-cluster",
	 * @return This operation returns created Cluster.
	 */
	public Cluster startCluster(String plugin_name, String hadoop_version,
			String cluster_template_id, String default_image_id,
			String user_keypair_id, String name) {
		RestCall caller = new RestCall();
		String payload = "{    \"plugin_name\": \"" + plugin_name
				+ "\",    \"hadoop_version\": \"" + hadoop_version
				+ "\",    \"cluster_template_id\": \"" + cluster_template_id
				+ "\",    \"default_image_id\": \"" + default_image_id
				+ "\",    \"user_keypair_id\": \"" + user_keypair_id
				+ "\",    \"name\": \"" + name
				+ "\",    \"cluster_configs\": {}}";

		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/clusters", payload, "POST", token);
		try {
			if (connection.getResponseCode() != 202)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			return gson.fromJson(((JSONObject) JsonPath.read(JSONreponse,
					"$.cluster")).toJSONString(), Cluster.class);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param dataSourceId
	 * 
	 *            Terminate existing cluster. This operation returns nothing.
	 */
	public boolean terminateCluster(String cluster_id) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/clusters/" + cluster_id, null, "DELETE", token);
		try {
			if (connection.getResponseCode() == 204)
				return true;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
